<template lang="pug">
.fo-editor
  mt-dialog(
    v-model="model"
    cancel-text="取消"
    size="large"
    :title="tileName"
    v-bind:onCancel="onCancel"
    v-bind:onClose="onClose"
    :fullscreen="true"
    hide-confirm
  )
    editor(ref="foEditor")
</template>

<script setup lang="ts">
import { editor } from "@scmt/common-components";
import { useApi } from "@scmt/core-api";
import { type SpecimenEntity } from "@scmt/api-lis-specimen";
import { useRequest } from "@scmt/common-hooks";
const foEditor = ref<InstanceType<typeof editor>>();
//添加 Props属性
const props = defineProps({
  selectRows: Object,
});
//定义事件
const emit = defineEmits(["handleQuery"]);

const $api = useApi();
const { request, data } = useRequest({
  templateByEquipmentId: async (equipmentId: string) => {
    return await $api.bdm.template.get(equipmentId);
  },
  //根据标本id查询临床诊断
  getspecimenResult: async (specimenId: string) => {
    return await $api.lis.specimen.getPortfolioProjectBySpecimenId(specimenId);
  },
  listAsOptions: () => {
    return $api.lis.specimen.category.listAsOptions();
  },
  listDoctorAsOptions: (val: string) =>
    $api.user.listDoctorAsOptions({ officeId: val }),
});

onMounted(() => {
  request("listAsOptions");
  request("listDoctorAsOptions", "");
});

const editingObj = ref<null | SpecimenEntity>(null);
const jsonValue = ref("");
const model = defineModel({
  default: false,
});
//弹框标题
const tileName = ref<null | String>("查看报告");

//关闭弹框
const onCancel = () => {
  model.value = false;
};
//取消
const onClose = () => {
  model.value = false;
};

/**
 * 查找模板数据
 * @param templatejson 模板json
 * @param key 查询的key
 * @param resKey  需要的返回值
 */
const quaryData = (templatejson: any, key: any, resKey: any): boolean => {
  let res = false;
  if (templatejson && key) {
    if (templatejson.content && templatejson.content.length > 0) {
      if (templatejson.attrs && key == templatejson.attrs.name) {
        resKey.value = templatejson.content[0].text;
        res = true;
        return res;
      }
      for (let index = 0; index < templatejson.content.length; index++) {
        const element = templatejson.content[index];
        let resFor = quaryData(element, key, resKey);
        if (resFor) {
          res = resFor;
          break;
        }
      }
    }
  }
  return res;
};

watch(
  model,
  () => {
    if (model.value) {
      request("listAsOptions");
      foEditor.value?.destroy();
      nextTick(() => {
        if (props.selectRows) {
          let item = props.selectRows;
          let equipmentId = "3722a7b8ecc30c4d13b85acf3e04badc";
          if (props.selectRows.applicationProject == "血常规") {
            equipmentId = "3722a7b8ecc30c4d1385acf3e04badc";
          } else if (props.selectRows.applicationProject == "尿常规") {
            equipmentId = "3722a7b8ecc380c4d1385acf3e04badc";
          } else {
            equipmentId = "3722a7b8ecc30c4d13b85acf3e04badc";
          }

          request("getspecimenResult", props.selectRows.id).then(() => {
            editingObj.value = $api.lis.specimen.$$createEntity(
              data.getspecimenResult ?? {}
            );
            item.diagnosticOpinion = ""; // 诊断意见
            if (editingObj.value.id) {
              Object.assign(item, {
                clinicalDiagnosis: editingObj.value.clinicalDiagnosis,
                name: editingObj.value.patientName,
                age: editingObj.value.age,
                sex: editingObj.value.sex,
                specimen: editingObj.value.specimenNumber,
                barcode: editingObj.value.barcode,
                specimenType: editingObj.value.specimenTypeName,
                doctorImage: editingObj.value.examineImgUrl,
                inspector:
                  data.listDoctorAsOptions?.find(
                    (i) => i.value == editingObj.value?.inspector
                  )?.label ?? editingObj.value?.inspector,
                sendInspectionTime: editingObj.value.sendInspectionTime,
                reviewer:
                  data.listDoctorAsOptions?.find(
                    (i) => i.value == editingObj.value?.reviewer
                  )?.label ?? editingObj.value?.reviewer,
                inspectionTime: editingObj.value.inspectionTime,
                sendInspectionId:
                  data.listDoctorAsOptions?.find(
                    (i) => i.value == editingObj.value?.sendInspectionId
                  )?.label ?? editingObj.value?.sendInspectionId,
              });
            }
            request("templateByEquipmentId", equipmentId).then(() => {
              let resData = data.templateByEquipmentId ?? {};
              let resItem = $api.bdm.template.$$createEntity(resData);
              if (resData && resItem.jsonTemplate) {
                //匹配数据
                let json = JSON.parse(resItem.jsonTemplate);
                item.Ttable = editingObj.value.specimenProjectList;
                jsonValue.value = JSON.stringify(json);
              }
              nextTick(() => {
                foEditor.value?.initEditor(
                  model.value,
                  jsonValue.value ?? JSON.parse(jsonValue.value),
                  item
                );
              });
            });
          });
        }
      });
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped>
.fo-editor {
  min-width: 980px;
  min-height: 600px;
}

:deep(.mt-dialog__modal) {
  height: 100vh !important;
}
</style>
